﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PMSGSC.DAL
{
   public class Employee_DAL:BaseDAL
    {
        // Lấy tất cả  Employee
        public static List<Employee> GetAll(int ShipID)
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB. Employees);
            return DB.Employees.Where(n => n.Department.Ship_ID == ShipID).ToList();
        }
        public static Employee TestLogin(string userName, string passWord)
        {
            return DB.Employees.Where(n => n.UserName.ToUpper() == userName.ToUpper()
                && n.PassWord.ToUpper() == passWord.ToUpper()).FirstOrDefault();
        }
        // Lấy  Employee theo Id
        public static  Employee GetDataById(int id)
        {
            return DB. Employees.Where(m => m.Employee_ID == id).FirstOrDefault();
        }
        //Thêm một  Employee
        public static bool Insert( Employee  Employee)
        {
            try
            {
                DB. Employees.InsertOnSubmit( Employee);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Xóa một  Employee
        public static bool Delete( Employee  Employee)
        {
            try
            {
                DB. Employees.DeleteOnSubmit( Employee);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Cập nhật  Employee
        public static bool Update( Employee  employee)
        {
            try
            {
                //Employee objEmployee = GetDataById(employee.Employee_ID);
                //objEmployee.CodeEmployee = employee.CodeEmployee;
                //objEmployee.Name = employee.Name;
                //objEmployee.Address = employee.Address;
                //objEmployee.Tell = employee.Tell;
                //objEmployee.Email = employee.Email;
                //objEmployee.UserName = employee.UserName;
                //objEmployee.PassWord = employee.PassWord;
                //objEmployee.Lock = employee.Lock;              
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }

        // Kiểm tra xem  Employee Code có tồn tại chưa ?
        public static bool IsExits( Employee  Employee)
        {
             Employee objEmployee = null;
            objEmployee = DB. Employees.Where(m => (m.CodeEmployee ==  Employee.CodeEmployee) && 
                (m.Employee_ID !=  Employee.Employee_ID) && (m.Department_ID == Employee.Department_ID)).FirstOrDefault();
            return objEmployee != null ? true : false;
        }
        public static bool IsRelationship(Employee employee)
        {
            EmployeeDecentralization employeeDecen = DB.EmployeeDecentralizations.Where(n => n.Employee_ID == employee.Employee_ID).FirstOrDefault();
            if (null != employeeDecen)
                return false;

            RescheduleLog reschedule = DB.RescheduleLogs.Where(n => n.Employee_ID == employee.Employee_ID).FirstOrDefault();
            if (null != reschedule)
                return false;

            Job job = DB.Jobs.Where(n => n.Employee_ID == employee.Employee_ID).FirstOrDefault();
            if (null != job)
                return false;

            return true;
        }
    }
}
